package com.press.controllers.admin;

import com.alibaba.fastjson.JSONObject;
import com.press.bean.OptionBean;
import com.press.bean.UserBean;
import com.press.controllers.InitController;
import com.press.services.InitService;
import com.press.services.SetService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2016/10/14.
 */
@Controller
@RequestMapping("/set")
public class SetController {

    private static Logger log = Logger.getLogger(SetController.class);
    @Resource( name = "setService")
    private SetService setService;


    //评论设置
    @RequestMapping("/commentSet")
    public void commentSet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{
        Map<String,String> para = new HashMap();
        para.put("allow_not_login",request.getParameter("comment_allow_not_login"));
        para.put("must_audited",request.getParameter("comment_must_audited"));
        request.getServletContext().setAttribute("comment",para);
        if("true".equals(request.getParameter("comment_allow_not_login"))){     //允许未登录用户评论

        }
        if("true".equals(request.getParameter("comment_must_audited"))){   //评论必须经过管理员审核

        }
        response.sendRedirect(request.getContextPath()+"/admin/settings/commentSet.jsp");

    }

    //常规设置
    @RequestMapping("/RegularSet")
    public void RegularSet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
       log.info("*******************************进入setController");
        UserBean adminUser = new UserBean();
        //更改管理员信息
        adminUser.setUser_id(((UserBean)request.getSession().getAttribute("admin")).getUser_id());
        adminUser.setUser_email(request.getParameter("email"));
        adminUser.setUser_phone(request.getParameter("phone"));
        adminUser.setUser_qq(request.getParameter("qq"));
        adminUser.setUser_weibo(request.getParameter("weibo"));
        adminUser = setService.adminSet(adminUser);
        //将管理员信息重新存在session中
        request.getSession().setAttribute("admin",adminUser);
        //更改站点名称
        String web_name = request.getParameter("web_name");
        setService.setName(web_name);
        InitController initController =(InitController) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean("initController");
        initController.init(request.getServletContext());
        WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean("initController");
        response.sendRedirect(request.getContextPath()+"/admin/settings/regularSet.jsp");


    }


    public SetService getSetService() {
        return setService;
    }

    public void setSetService(SetService setService) {
        this.setService = setService;
    }


}
